<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="启用状态" prop="openStatus">
        <el-select v-model="queryParams.openStatus" placeholder="请选择启用状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fun:programConfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fun:programConfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fun:programConfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fun:programConfig:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="programConfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="配置ID" align="center" prop="configId" width="80" />
      <!-- <el-table-column label="组织ID" align="center" prop="orgId" width="80" /> -->
      <el-table-column label="轮播图显示" align="center" prop="showBanner" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.showBanner == 1 ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公告显示" align="center" prop="showNotice" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.showNotice == 1 ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主题色" align="center" prop="themeColor" width="100">
        <template slot-scope="scope">
          <span :style="{fontWeight: 'bold'}">{{ getThemeColorName(scope.row.themeColor) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺名称" align="center" prop="aboutShopName" width="120" />
      <el-table-column label="软件版本" align="center" prop="aboutVersion" width="100" />
      <el-table-column label="启用状态" align="center" prop="openStatus" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.openStatus"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fun:programConfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fun:programConfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { listProgramConfig, getProgramConfig, delProgramConfig, addProgramConfig, updateProgramConfig } from "@/api/fun/programConfig"

export default {
  name: "ProgramConfig",
  dicts: ['sys_yes_no', 'ec_program_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小程序配置表格数据
      programConfigList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openStatus: null,
      },

    }
  },
  created() {
    this.getList()
  },
  mounted() {
    // 监听表单提交事件
    this.$eventBus.$on('refreshProgramConfigList', () => {
      this.getList()
    })
  },
  beforeDestroy() {
    // 移除事件监听器
    this.$eventBus.$off('refreshProgramConfigList')
  },
  methods: {
    /** 查询小程序配置列表 */
    getList() {
      this.loading = true
      listProgramConfig(this.queryParams).then(response => {
        this.programConfigList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      const obj = { path: "/fun/programConfig/form", query: { pageType: "add" } };
      this.$tab.openPage("新增小程序配置", obj.path, obj.query);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const configId = row.configId || this.ids;
      const obj = { path: "/fun/programConfig/form", query: { pageType: "edit", configId: configId } };
      this.$tab.openPage("修改小程序配置", obj.path, obj.query);
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.configId || this.ids
      this.$modal.confirm('是否确认删除小程序配置编号为"' + configIds + '"的数据项？').then(function() {
        return delProgramConfig(configIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fun/programConfig/export', {
        ...this.queryParams
      }, `programConfig_${new Date().getTime()}.xlsx`)
    },
    /** 获取主题色名称 */
    getThemeColorName(value) {
      const colorMap = {
        '0': '蓝色',
        '1': '绿色', 
        '2': '橙色',
        '3': '黑色',
        '4': '紫色'
      }
      return colorMap[value] || '未知'
    },
    /** 处理启用状态变化 */
    handleStatusChange(row) {
      const text = row.openStatus === 1 ? "启用" : "停用"
      this.$modal.confirm('确认要"' + text + '""' + row.aboutShopName + '"配置吗？').then(() => {
        return updateProgramConfig(row)
      }).then(() => {
        this.$modal.msgSuccess(text + "成功")
      }).catch(() => {
        row.openStatus = row.openStatus === 0 ? 1 : 0
      })
    }
  }
}
</script>
